home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / CDragAndDrop / CDragItem.cp < prev    next >
Encoding:
Text File  |  1995-02-09  |  9.2 KB  |  320 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CDragItem.cp            ©1995 J. Rodden, DD/MF & Associates. All rights reserved
  3. // =================================================================================
  4. // Mixin class to be used with anything that supports dragging of it's items.
  5. // CDragItem encapsulates the characteristics of a dragable item.
  6. //
  7. // By using CDragItem and overriding AddFlavors and MakeDragRegion, you can
  8. // (a) include as many flavors of an item as you want, (b) define your own outline
  9. // to replace the generic rectangle that LDragTask provides, (c) arrange to send 
  10. // drag data on demand by calling InstallDragSendData when adding flavors (and 
  11. // installing null data in the drag) and overriding DoSendDragData for the object 
  12. // being draged, and (d) override the drag manager's default behaviors for DragInput 
  13. // and DragDrawing by calling InstallDragInput or InstallDragDrawing and overriding 
  14. // DoDragInput or DoDragDrawing respectively.
  15. // =================================================================================
  16.  
  17. #include "CDragItem.h"
  18.  
  19. // ============================================================================
  20. //        • AddFlavors
  21. // ============================================================================
  22. //    Add flavored items to the DragTask.
  23.  
  24. void
  25. CDragItem::AddFlavors(DragReference inDragRef)
  26. {
  27. }
  28.  
  29.  
  30. // ============================================================================
  31. //        • MakeDragRegion
  32. // ============================================================================
  33. //    Build the region outlining the items to be dragged
  34.  
  35. void
  36. CDragItem::MakeDragRegion(
  37.     DragReference    inDragRef,
  38.     RgnHandle        inDragRegion)
  39. {
  40.     RgnHandle        theDragRgn = ::NewRgn();    // Make region containing item
  41.  
  42.   ::OpenRgn();
  43.     SetLocalFrame();
  44.     DrawDragRegion();                            // Draw directly into the drag region
  45.   ::CloseRgn (theDragRgn);
  46.  
  47.     OutlineRegion(theDragRgn);                    // Carve out interior of region
  48.     
  49.     RgnHandle    extraRgn = ::NewRgn ();
  50.     if (AddToDragRgn(extraRgn)) {                // provide hook to add another region
  51.         OutlineRegion(extraRgn);
  52.       ::UnionRgn (extraRgn, theDragRgn, theDragRgn);
  53.     }
  54.   ::DisposeRgn (extraRgn);
  55.  
  56.     // Accumulate new regions in our total drag region
  57.   ::UnionRgn (theDragRgn, inDragRegion, inDragRegion);
  58.     
  59.     // Tell Drag Manager about this item
  60.   ::SetDragItemBounds (inDragRef, kItemRef, &(**theDragRgn).rgnBBox);
  61.   ::DisposeRgn (theDragRgn);
  62. }
  63.  
  64.  
  65. // ============================================================================
  66. //        • SetLocalFrame
  67. // ============================================================================
  68. // Setup local frame (in global coords) for reference by drawing routines
  69.  
  70. void
  71. CDragItem::SetLocalFrame (void)
  72. {
  73. }
  74.  
  75.  
  76. // ============================================================================
  77. //        • DrawDragRegion
  78. // ============================================================================
  79. // Draw into the drag region, default does nothing
  80.  
  81. void
  82. CDragItem::DrawDragRegion ()
  83. {
  84. }
  85.  
  86.  
  87. // ============================================================================
  88. //        • AddToDragRgn
  89. // ============================================================================
  90. // Add a region to the drag region, default does nothing (automatically outlined)
  91. //    return true to include changes to inDragRegion to total drag region.
  92.  
  93. Boolean
  94. CDragItem::AddToDragRgn (RgnHandle inDragRegion)
  95. {
  96.     return false;
  97. }
  98.  
  99. // ============================================================================
  100. //        • OutlineRegion
  101. // ============================================================================
  102. // Utility for obtaining the outline of a region
  103.  
  104. void
  105. CDragItem::OutlineRegion(RgnHandle inRegion)
  106. {
  107.     RgnHandle    innerRgn = ::NewRgn();    // Carve out interior of region so
  108.   ::CopyRgn (inRegion, innerRgn);        //   that it's just a one-pixel thick
  109.   ::InsetRgn (innerRgn, 1, 1);            //   outline of the item
  110.   ::DiffRgn (inRegion, innerRgn, inRegion);
  111.   ::DisposeRgn (innerRgn);
  112. }
  113.  
  114.  
  115. // ============================================================================
  116. //        • DoDragSendData
  117. // ============================================================================
  118. //    Send the data associated with a particular drag item
  119. //
  120. //    This function gets called if you installed the optional DragSendDataProc
  121. //    for this DropArea. In which case you should override this function
  122. //    to provide the requested data by calling SetDragItemFlavorData.
  123.  
  124. void
  125. CDragItem::DoDragSendData(
  126.     FlavorType        inFlavor,
  127.     ItemReference    inItemRef,
  128.     DragReference    inDragRef)
  129. {
  130. }
  131.  
  132.  
  133. // ============================================================================
  134. //        • DoDragInput
  135. // ============================================================================
  136. //    Modify the state of the mouse and modifier keys during a drag
  137. //
  138. //    This function gets called if you installed the optional DragInputProc
  139. //    for this DropArea.
  140.  
  141. void
  142. CDragItem::DoDragInput(
  143.     Point            *ioMouse,
  144.     Int16            *ioModifiers,
  145.     DragReference    inDragRef)
  146. {
  147. }
  148.  
  149.  
  150. // ============================================================================
  151. //        • DoDragDrawing
  152. // ============================================================================
  153. //    Draw the items for a drag in progress
  154. //
  155. //    This function gets called if you installed the optional DragDrawingProc
  156. //    for this DropArea.
  157.  
  158. void
  159. CDragItem::DoDragDrawing(
  160.     DragRegionMessage    inMessage,
  161.     RgnHandle            inShowRgn,
  162.     Point                inShowOrigin,
  163.     RgnHandle            inHideRgn,
  164.     Point                inHideOrigin,
  165.     DragReference         inDragRef)
  166. {
  167. }
  168.  
  169.  
  170. // ============================================================================
  171. // • Install Callback Functions                       Install Callback Functions •
  172. // ============================================================================
  173. //    Install SendData, DragInput, and DragDrawing Handlers for the Drag Manager
  174. //
  175. //    We use a single handler for each of the Drag and Drop operations.
  176. //    With the Drag Manager, you can register handlers globally or for specific
  177. //    windows. With PowerPlant, we want to have separate handlers for each
  178. //    dragable object.
  179.  
  180. // ============================================================================
  181. //        • InstallDragSendData
  182. // ============================================================================
  183.  
  184. void
  185. CDragItem::InstallDragSendData(DragReference inDragRef)
  186. {
  187.     OSErr    err;
  188.  
  189.     DragSendDataUPP    theDragSendDataUPP = NewDragSendDataProc(HandleDragSendData);
  190.  
  191.     err = ::SetDragSendProc( inDragRef, theDragSendDataUPP, this);
  192.     ThrowIfOSErr_(err);
  193. }
  194.  
  195.  
  196. // ============================================================================
  197. //        • InstallDragInput
  198. // ============================================================================
  199.  
  200. void
  201. CDragItem::InstallDragInput(DragReference inDragRef)
  202. {
  203.     OSErr    err;
  204.  
  205.     DragInputUPP    theDragInputUPP = NewDragInputProc(HandleDragInput);
  206.  
  207.     err = ::SetDragInputProc( inDragRef, theDragInputUPP, this);
  208.     ThrowIfOSErr_(err);
  209. }
  210.  
  211.  
  212. // ============================================================================
  213. //        • InstallDragDrawing
  214. // ============================================================================
  215.  
  216. void
  217. CDragItem::InstallDragDrawing(DragReference inDragRef)
  218. {
  219.     OSErr    err;
  220.  
  221.     DragDrawingUPP    theDragDrawingUPP = NewDragDrawingProc(HandleDragDrawing);
  222.  
  223.     err = ::SetDragDrawingProc( inDragRef, theDragDrawingUPP, this);
  224.     ThrowIfOSErr_(err);
  225. }
  226.  
  227.  
  228. // ===========================================================================
  229. // • Static Callback Functions                       Static Callback Functions •
  230. // ===========================================================================
  231.  
  232. // ============================================================================
  233. //        • HandleDragSendData
  234. // ============================================================================
  235. //    Drag Manager callback for sending the data for an item that is part
  236. //    of an accepted drag and drop
  237.  
  238. pascal OSErr
  239. CDragItem::HandleDragSendData(
  240.     FlavorType        inFlavor,
  241.     void            *inRefCon,
  242.     ItemReference    inItemRef,
  243.     DragReference    inDragRef)
  244. {
  245.     OSErr    err = noErr;
  246.  
  247.     Try_ {
  248.         ((CDragItem*) inRefCon)->DoDragSendData(inFlavor, inItemRef,
  249.                                                     inDragRef);
  250.     }
  251.     
  252.     Catch_(inErr) {
  253.         err = inErr;
  254.     } EndCatch_
  255.     
  256.     return err;
  257. }
  258.  
  259.  
  260. // ============================================================================
  261. //        • HandleDragInput
  262. // ============================================================================
  263. //    Drag Manager callback for manipulating the mouse and modifier keys
  264. //    during a drag
  265.  
  266. pascal OSErr
  267. CDragItem::HandleDragInput(
  268.     Point            *ioMouse,
  269.     Int16            *ioModifiers,
  270.     void            *inRefCon,
  271.     DragReference    inDragRef)
  272. {
  273.     OSErr    err = noErr;
  274.  
  275.     Try_ {
  276.         ((CDragItem*) inRefCon)->DoDragInput(ioMouse, ioModifiers,
  277.                                                     inDragRef);
  278.     }
  279.     
  280.     Catch_(inErr) {
  281.         err = inErr;
  282.     } EndCatch_
  283.     
  284.     return err;
  285. }
  286.  
  287.  
  288. // ============================================================================
  289. //        • HandleDragDrawing
  290. // ============================================================================
  291. //    Drag Manager callback for drawing the items during a drag
  292.  
  293. pascal OSErr
  294. CDragItem::HandleDragDrawing(
  295.     DragRegionMessage    inMessage,
  296.     RgnHandle            inShowRgn,
  297.     Point                inShowOrigin,
  298.     RgnHandle            inHideRgn,
  299.     Point                inHideOrigin,
  300.     void                *inRefCon,
  301.     DragReference        inDragRef)
  302. {
  303.     OSErr    err = noErr;
  304.  
  305.     Try_ {
  306.         ((CDragItem*) inRefCon)->DoDragDrawing(inMessage,
  307.                                                 inShowRgn, inShowOrigin,
  308.                                                 inHideRgn, inHideOrigin,
  309.                                                 inDragRef);
  310.     }
  311.     
  312.     Catch_(inErr) {
  313.         err = inErr;
  314.     } EndCatch_
  315.     
  316.     return err;
  317. }
  318.  
  319.  
  320.